//
//  CJSNavigationView.m
//  GoTraveling
//
//  Created by 陈金山 on 15/12/23.
//  Copyright © 2015年 开发华佗. All rights reserved.
//

#import "CJSNavigationView.h"

@interface CJSNavigationView ()

@property (nonatomic, strong) UIButton *leftButton;

@property (nonatomic, strong) UIButton *rightButton;

@property (nonatomic, strong) UIButton *searchButton;

@property (nonatomic, strong) UIButton *beforeButton;

@property (nonatomic, strong) UIView *selectView;

@property (nonatomic, strong) NSMutableArray *btnArray;

@end

@implementation CJSNavigationView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithRed:0.18 green:0.21 blue:0.24 alpha:1];
        [self createSubViews];
    }
    return self;
}

-(void)createSubViews
{
    self.btnArray = [NSMutableArray array];
    
    self.leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.leftButton.frame = CGRectMake(0, 0, self.frame.size.width / 2, self.frame.size.height);
    self.leftButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [self.leftButton setTitle:@"精选" forState:UIControlStateNormal];
    [self.leftButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.leftButton.tag = 1000;
    [self.leftButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.leftButton];
    [self.btnArray addObject:self.leftButton];
    self.leftButton.transform = CGAffineTransformMakeScale(1.15, 1.15);
    
    self.rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.rightButton.frame = CGRectMake(self.frame.size.width / 2, 0, self.frame.size.width / 2, self.frame.size.height);
    self.rightButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [self.rightButton setTitle:@"热门" forState:UIControlStateNormal];
    [self.rightButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    self.rightButton.tag = 1001;
    [self.rightButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.rightButton];
    [self.btnArray addObject:self.rightButton];
    
    self.beforeButton = self.leftButton;
    
    self.selectView = [[UIView alloc] initWithFrame:CGRectMake(0, self.frame.size.height - 5, self.frame.size.width / 2, 5)];
    self.selectView.backgroundColor = [UIColor colorWithRed:0.156 green:0.803 blue:0.607 alpha:1.000];
    [self addSubview:self.selectView];
    
}

- (void)buttonAction:(UIButton *)button {
    
    if (![button isEqual:self.beforeButton]) {
        [self.beforeButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        CGRect frame = self.selectView.frame;
        frame.origin.x = button.frame.origin.x;
        
        [UIView animateWithDuration:0.2 animations:^{
            
            self.selectView.frame = frame;
            self.beforeButton.transform = CGAffineTransformMakeScale(1, 1);
            button.transform = CGAffineTransformMakeScale(1.15, 1.15);
        }];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        self.beforeButton = button;
        
        [self.delegate changeScrollOffset:button.tag - 1000];
    }
}

- (void)setSelectNum:(NSInteger)selectNum {
    
    UIButton *button = [self.btnArray objectAtIndex:selectNum];
    
    if (![button isEqual:self.beforeButton]) {
        [self.beforeButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        CGRect frame = self.selectView.frame;
        frame.origin.x = button.frame.origin.x;
        
        [UIView animateWithDuration:0.2 animations:^{
            
            self.selectView.frame = frame;
            self.beforeButton.transform = CGAffineTransformMakeScale(1, 1);
            button.transform = CGAffineTransformMakeScale(1.15, 1.15);
        }];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        self.beforeButton = button;
    }
    
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
